<template>
  <div class="home-module__dropdown" @mouseenter="showClassify" @mouseleave="hideClassify">
    <div v-if="categoryList.length > 0" class="home-module__dropdown-menu">
      <div
        class="home-module__dropdown-menu-item"
        :class="{ active: indexActive == item.id }"
        v-for="(item, index) in categoryList.slice(0, 5)"
        :key="index"
        @mouseenter="switchClassify(item)"
      >
        <div class="signal" @click="goCourseCategory(item.id)">
          <span class="title">{{ item.title }}</span>
          <i class="el-icon-arrow-right el-icon--right"></i>
        </div>
        <div class="parallel" v-if="$util.isDef(item.child) && item.child.length > 0">
          <span
            v-for="(itemchild, index) in item.child.slice(0, 6)"
            :key="index"
            class="item"
            @click="goCourseCategory(itemchild.id)"
          >
            {{ itemchild.title }}
          </span>
        </div>
      </div>
    </div>
    <transition name="el-fade-in">
      <div v-show="indropdownMain" class="home-module__dropdown-body">
        <div class="dropdown-wrap">
          <div class="dropdown-list" v-for="datas in dropdownList" :key="datas.id">
            <div class="signal" @click="goCourseCategory(datas.id)">{{ datas.title }}</div>
            <div class="parallel">
              <div
                v-for="item in datas.child"
                :key="item.id"
                class="item"
                @click="goCourseCategory(item.id)"
              >
                {{ item.title }}
              </div>
            </div>
          </div>
        </div>
        <div class="course-wrap" v-if="courseList.length > 0">
          <div class="course-wrap__title">推荐课程</div>
          <div class="course-wrap__list">
            <div class="item" v-for="item in courseList" :key="item.id" @click="goCourse(item.id)">
              <EduCourseImage
                :image="item.cover_url"
                :course-type="item.course_type"
                :border-radius="4"
                :height="106"
                class="hvr-float"
                lazy
              ></EduCourseImage>
              <div class="title">{{ item.title }}</div>
            </div>
          </div>
        </div>
      </div>
    </transition>
  </div>
</template>
<script>
import EduCourseImage from '@/components/EduCourseImage'
export default {
  components: {
    EduCourseImage
  },
  data() {
    return {
      indropdownMain: false,
      indexActive: 0,
      categoryList: [],
      dropdownList: [],
      courseList: []
    }
  },
  mounted() {
    this.handleCategoryInit()
  },
  methods: {
    // 鼠标移入显示更多分类列表
    showClassify() {
      this.indropdownMain = true
    },
    // 鼠标移出初始化分类列表
    hideClassify() {
      this.indropdownMain = false
      setTimeout(() => {
        this.indexActive = 0
      }, 300)
    },
    // 鼠标移入切换分类列表
    switchClassify(item) {
      this.indexActive = item.id
      if (this.$util.isUndef(item.child)) {
        this.dropdownList = []
      } else {
        this.dropdownList = item.child
      }
      this.courseList = item.splendid_course
    },
    // 分类
    handleCategoryInit() {
      this.$api.getCategory(
        {
          type: 0
        },
        res => {
          this.categoryList = res.data
        }
      )
    },
    // 跳转
    goCourseCategory(id) {
      this.$router.push({
        path: '/course',
        query: {
          category: id
        }
      })
    },
    // 跳转课程
    goCourse(id) {
      this.$router.push({
        path: '/course/' + id
      })
    }
  }
}
</script>
<style lang="scss" scoped>
@import '@/styles/hover.scss';
.home-module__dropdown {
  position: relative;
  flex-basis: 270px;
  &-menu {
    width: 270px;
    height: 500px;
    padding: 10px 0;
    position: absolute;
    left: 0;
    top: 0;
    z-index: 1000;
    background-color: #ffffff;
    box-sizing: border-box;
    &-item {
      padding: 13px 15px;
      @include prefixed(transition-duration, 0.3s);
      border-left-style: solid;
      border-left-width: 3px;
      border-left-color: transparent;
      box-sizing: border-box;
      &.active {
        @include edu-color-primary(background-color, 0.04);
        @include edu-color-primary(border-left-color);
      }
      .signal {
        display: flex;
        align-items: center;
        cursor: pointer;
        .title {
          flex: 1;
          min-width: 0;
          font-size: 14px;
          font-weight: bold;
          color: $text-color-import;
          cursor: pointer;
          line-height: 21px;
          @include ellipsis;
        }
        .el-icon-arrow-right {
          color: #b7bac1;
          font-size: 12px;
        }
      }

      .parallel {
        display: flex;
        flex-wrap: wrap;
        margin-top: 7px;
        margin-left: -20px;
        .item {
          margin-top: 4px;
          margin-left: 20px;
          font-size: 12px;
          color: $text-color-secondary;
          line-height: 17px;
          cursor: pointer;
          max-width: 60px;
          @include ellipsis;
        }
      }
    }
  }
  &-body {
    width: 930px;
    min-height: 500px;
    position: absolute;
    left: 270px;
    top: 0;
    z-index: 999;
    padding: 32px 20px;
    box-sizing: border-box;
    background: #ffffff;
    box-shadow: -2px 2px 10px 0px rgba(48, 49, 51, 0.05);
    .dropdown-wrap {
      min-height: 235px;
      .dropdown-list {
        display: flex;
        align-items: flex-start;
        margin-bottom: 22px;
        .signal {
          flex: 0 0 166px;
          font-size: 14px;
          font-weight: bold;
          color: $text-color-import;
          line-height: 18px;
          cursor: pointer;
          margin-bottom: 10px;
          @include ellipsis();
        }
        .parallel {
          flex: 1;
          min-width: 0;
          display: flex;
          flex-wrap: wrap;
          .item {
            width: 161px;
            font-size: 13px;
            color: $text-color-secondary;
            line-height: 17px;
            margin-bottom: 10px;
            margin-left: 20px;
            cursor: pointer;
            @include ellipsis();
          }
        }
      }
    }
    .course-wrap {
      &__title {
        font-size: 14px;
        font-weight: 500;
        color: #909399;
        line-height: 21px;
      }
      &__list {
        margin-top: 15px;
        display: grid;
        grid-gap: 0 30px;
        grid-template-columns: repeat(4, minmax(0px, 1fr));
        .item {
          grid-column: span 1 / span 1;
          .title {
            margin-top: 12px;
            font-size: 14px;
            font-weight: 400;
            color: $text-color-import;
            line-height: 21px;
            cursor: pointer;
            @include ellipsis;
          }
        }
      }
    }
  }
}
</style>
